package com.fruit.account.service.commission;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.alibaba.fastjson.JSONObject;
import com.fruit.account.common.CommMsgUtil;
import com.fruit.account.service.excommission.CommBackLogServiceImpl;
import com.fruit.mapper.account.balance.BalanceDetailMapper;
import com.fruit.mapper.account.buybackrule.BuyBackRuleMapper;
import com.fruit.mapper.account.commission.CommissionDetailMapper;
import com.fruit.mapper.account.transaction.TransactionFlowPoMapper;
import com.fruit.mapper.account.user.UserAccountMapper;
import com.fruit.pojo.account.balance.BalanceDetail;
import com.fruit.pojo.account.buybackrule.BuyBackRule;
import com.fruit.pojo.account.commission.CommissionDetail;
import com.fruit.pojo.account.excommission.ExceptionCommission;
import com.fruit.pojo.account.transaction.TransactionFlowPo;
import com.fruit.pojo.account.user.UserAccount;
import com.fruit.pojo.user.UserInfo;
import com.fruit.pojo.user.sysmsg.SysMessage;
import com.fruit.sdk.account.commission.CommissionBackService;
import com.fruit.service.sms.SmsCodeManager;
import com.fruit.service.sysmsg.SysMessageService;
import com.fruit.service.user.UserManager;
import com.fruit.util.CustomUtil;
import com.fruit.util.DateUtil;
import com.fruit.util.Exceptions;
import com.fruit.util.ResultInfo;
import com.fruit.util.ResultUtil;
/**
 * 返销业务相关Service
 * @author Administrator
 *
 */
@Service("commissionBackService")
public class CommissionBackServiceImpl implements CommissionBackService {
	protected Logger logger = LoggerFactory.getLogger(CommissionBackServiceImpl.class);
	@Autowired
	private BuyBackRuleMapper buyBackRuleMapper;
	@Autowired
	private UserManager userService;
	@Autowired
	private TransactionFlowPoMapper transactionFlowPoMapper;
	@Autowired
	private CommissionDetailMapper commissionDetailMapper;
	@Autowired
	private BalanceDetailMapper balanceDetailMapper;
	@Autowired
	private UserAccountMapper userAccountMapper;
	@Autowired
	private CommBackLogServiceImpl commBackLogServiceImpl;
	@Autowired
	private SysMessageService sysMessageService;
	@Autowired
	private SmsCodeManager smsCodeService;
	/**
	 * 返销业务 
	 * 1、判断返销规则 2、获取返销用户（当前用户的上下级）3、生成返销交易订单 4、生成返销记录 5、生成余额明细 6、更新账户余额
	 */
	@Override
	@SuppressWarnings("unchecked")
	public Map<String, Object> insertCommBack(String tradeId,Integer userId, BigDecimal money) {
		Map<String, Object> result = new HashMap<String, Object>();
		ExceptionCommission exc=new ExceptionCommission();
		try {
			logger.info("["+tradeId+"]---【返销处理】---【开始inputparams】---userId=" + userId + ";money=" + money);
			if (userId == null) {
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入参数用户id不能为空!"));
				return result;
			}
			if (money == null ||money.doubleValue()<=0) {
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入参数支付金额必须大于零!"));
				return result;
			}
			if (StringUtils.isEmpty(tradeId)) {
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "日志流水号不能为空!"));
				return result;
			}
			
			// 1、触发耳机分销 获取返销规则
			BuyBackRule recRule = new BuyBackRule();
			recRule.setState("01");// 已启用
			recRule.setStartTime(DateUtil.getTime());
			BuyBackRule bbRule = buyBackRuleMapper.selectByCondition(recRule);
			if (bbRule != null) {// 规则存在
				logger.info("["+tradeId+"]---【返销处理】---【规则参数】-----" + bbRule.toString());
				// 一级返销点数，二级返销点数如果获取不到，则不执行佣金返销功能
				if (bbRule.getOneLevelRebate() == null) {
					result.put("result", (ResultInfo) ResultUtil.initResult(ResultInfo.ERROR, "backrule_error", "获取一级返销规则点数异常！", "", "01"));
					return result;
				}
				if (bbRule.getTwoLevelRebate() == null) {
					result.put("result", (ResultInfo) ResultUtil.initResult(ResultInfo.ERROR, "backrule_error", "获取二级返销规则点数异常！", "", "01"));
					return result;
				}
				// 2、获取用户上下级 调用户中心的服务
				Map<String, Object> userMap = null;
				//一级用户对象
				UserInfo parentOne=null;
				//二级用户对象
				UserInfo parentTow=null;
				try {
					logger.info("["+tradeId+"]---【返销处理】---【获取用户上下级】-----");
					userMap = userService.getUserParent(userId);
					if (userMap == null) {
						result.put("result",(ResultInfo) ResultUtil.initResult(ResultInfo.ERROR, "user_error", "获取到用户上级失败", "", "01"));
						return result;
					}
					ResultInfo userRi = (ResultInfo) userMap.get("result");
					if (ResultInfo.ERROR.equals(userRi.getState())) {
						result.put("result",(ResultInfo) ResultUtil.initResult(ResultInfo.ERROR, "user_error", "获取用户上级异常！", "", "01"));
						return result;
					}
					parentOne=(UserInfo) userMap.get("parentOne");
					parentTow=(UserInfo) userMap.get("parentTow");
					if (parentOne == null&&parentTow==null) {
						result.put("result", (ResultInfo) ResultUtil.initResult(ResultInfo.SUCCESS, "user_error", "未匹配到当前用户的上级关系"));
						return result;
					}
					logger.info("["+tradeId+"]---【返销处理】---【获取用户上下级id】-----" + userMap.toString());
				} catch (Exception e) {
					logger.error("["+tradeId+"]---【返销处理】---【获取用户上下级异常】-----" + Exceptions.getStackTraceAsString(e));
					result.put("result", (ResultInfo) ResultUtil.initResult(ResultInfo.ERROR, "user_exception", "获取用户上级异常！", "", "01"));
					return result;
				}
				// 3、构建返销交易订单
				List<TransactionFlowPo> tflist = new ArrayList<TransactionFlowPo>();
				// 保存交易订单
				result=saveTransactionFlow(tflist, bbRule,parentOne,parentTow,money);
				ResultInfo tranResult = (ResultInfo) result.get("result");
				if (ResultInfo.ERROR.equals(tranResult.getState())) {
					result.put("result", (ResultInfo) ResultUtil.initResult(ResultInfo.ERROR, "tranflow_error", "生成交易订单异常！", "", "01"));
					return result;
				}
				// 构造佣金返销记录
				List<CommissionDetail> Commlist = new ArrayList<CommissionDetail>();
				// 构建返销余额明细
				List<BalanceDetail> balancelist = new ArrayList<BalanceDetail>();
				// 构建修改用户余额
				List<UserAccount> uList = new ArrayList<UserAccount>();
				// 根据交易订单 构造余额明细、返销记录、修改用户余额
				result = createInfoByTranFlow(Commlist, balancelist, uList, tflist, bbRule, parentOne,parentTow, userId);
				ResultInfo creResult = (ResultInfo) result.get("result");
				if (ResultInfo.ERROR.equals(creResult.getState())) {
					result.put("result", (ResultInfo) ResultUtil.initResult(ResultInfo.ERROR, "cibtf_error", "创建余额明细、返销记录、修改用户余额异常！", "", "01"));
					return result;
				}
				// 4、生成返销记录
				result=insertCommission(Commlist);
				ResultInfo commResult = (ResultInfo) result.get("result");
				if (ResultInfo.ERROR.equals(commResult.getState())) {
					result.put("result", (ResultInfo) ResultUtil.initResult(ResultInfo.ERROR, commResult.getCode(), "生成返销记录异常！", "", "01"));
					return result;
				}
				// 5、生成余额明细
				result=insertBalance(balancelist);
				ResultInfo baResult = (ResultInfo) result.get("result");
				if (ResultInfo.ERROR.equals(baResult.getState())) {
					result.put("result", (ResultInfo) ResultUtil.initResult(ResultInfo.ERROR, baResult.getCode(), "生成余额明细异常！", "", "01"));
					return result;
				}
				// 6、修改用户账户余额
				result=updateUserAccount(uList);
				ResultInfo userResult = (ResultInfo) result.get("result");
				if (ResultInfo.ERROR.equals(userResult.getState())) {
					result.put("result", (ResultInfo) ResultUtil.initResult(ResultInfo.ERROR, userResult.getCode(), "创建余额明细、返销记录、修改用户余额异常！", "", "01"));
					return result;
				}
				//7、消息通知
				sendMsg(tflist,parentOne,parentTow,userId);
				//8、发送短信
				sendSms(tflist);
				result.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "comback_success", "返销业务操作成功"));
			} else {// 规则不存在
				logger.info("["+tradeId+"]---【返销处理】---【未匹配到返销业务规则】-----");
				result.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "backrule_error", "未匹配到返销业务规则"));
			}
			return result;
		} catch (Exception e) {
			logger.error("["+tradeId+"]---【返销处理】---【异常】-----" + Exceptions.getStackTraceAsString(e));
			result.put("result", (ResultInfo) ResultUtil.initResult(ResultInfo.ERROR, "comback_exception", "获取分销规则异常！", "", "01"));
			return result;
		} finally {
			ResultInfo ri = (ResultInfo) result.get("result");
			if (ResultInfo.ROLLBACK.equals(ri.getIsRollBack())) {
				logger.info("["+tradeId+"]---【返销处理】---【异常事务回滚】---{}---{}",ri.getMessage(),ri.getCode());
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
			if(ResultInfo.ERROR.equals(ri.getState())){
				logger.info("["+tradeId+"]---【返销处理】---【日志记录】---{}---{}",ri.getMessage(),ri.getCode());
				//设置异常记录信息
				exc.setTradeId(tradeId);
				exc.setUserId(userId);
				exc.setServiceName("CommissionBackServiceImpl");
				JSONObject paJson=new JSONObject();
				paJson.put("tradeId", tradeId);
				paJson.put("userId", userId);
				paJson.put("money", money);
				exc.setInputParams(paJson.toString());
				exc.setQueryTime(DateUtil.getTime());
				exc.setErrorCode(ri.getCode());
				exc.setErrorMessage(ri.getMessage());
				commBackLogServiceImpl.newCommBackLog(exc);
			}
			logger.info("["+tradeId+"]---【返销处理】---【结束】-----");
		}
	}
	/**
	 * 生成返销记录
	 * @param balancelist
	 * @return
	 */
	public Map<String, Object> insertCommission(List<CommissionDetail> Commlist){
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			logger.info("【佣金返销记录】---【开始】---");
			if (Commlist.size() <= 0) {
				logger.error("【佣金返销记录】---【当前用户不存在上级】-----");
				result.put("result",(ResultInfo) ResultUtil.initResult(ResultInfo.ERROR, "commiss_error", "返销记录异常！", "", "01"));
				return result;
			}
			logger.info("【佣金返销记录】---【saveparams】-----" + Commlist.toString());
			// 保存返销记录
			commissionDetailMapper.insertCommissionDetails(Commlist);
			result.put("result", (ResultInfo) ResultUtil.initResult(ResultInfo.SUCCESS, "commiss_success", "生成返销记录成功！"));
			logger.info("【佣金返销记录】---【结束】-----");
			return result;
		} catch (Exception e) {
			logger.error("【佣金返销记录】---【生成返销记录异常！】-----" + Exceptions.getStackTraceAsString(e));
			result.put("result", (ResultInfo) ResultUtil.initResult(ResultInfo.ERROR, "commiss_exception", "生成返销记录异常！", "", "01"));
			return result;
		}
	}
	/**
	 * 保存返销余额明细
	 * @param balancelist
	 * @return
	 */
	public Map<String, Object> insertBalance(List<BalanceDetail> balancelist){
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			logger.info("【返销余额明细】---【开始】-----");
			if (balancelist.size() <= 0) {
				logger.error("【返销余额明细】---【构建返销余额明细异常】-----" + balancelist.toString());
				result.put("result", (ResultInfo) ResultUtil.initResult(ResultInfo.ERROR, "balance_error", "构建返销余额明细异常！", "", "01"));
				return result;
			}
			logger.info("【返销余额明细】---【saveparams】-----" + balancelist.toString());
			// 保存余额明细
			balanceDetailMapper.insertBalanceDetails(balancelist);
			
			result.put("result", (ResultInfo) ResultUtil.initResult(ResultInfo.SUCCESS, "balance_success", "生成返销记录余额明细成功！"));
			logger.info("【返销余额明细】---【结束】-----");
			return result;
		} catch (Exception e) {
			logger.error("【返销余额明细】---【生成返销记录余额明细异常！】-----" + Exceptions.getStackTraceAsString(e));
			result.put("result", (ResultInfo) ResultUtil.initResult(ResultInfo.ERROR, "balance_exception", "生成返销记录余额明细异常！", "", "01"));
			return result;
		}
	}
	/**
	 * 修改用户余额
	 * @param uList 用户集合
	 * @return
	 */
	public Map<String, Object> updateUserAccount(List<UserAccount> uList){
		Map<String, Object> result = new HashMap<String, Object>();
		logger.info("【用户账户余额修改】---【开始】-----");
		try {
			if (uList.size() <= 0) {
				logger.error("【用户账户余额修改】---【构建参数异常】----");
				result.put("result", (ResultInfo) ResultUtil.initResult(ResultInfo.ERROR, "useracc_error",
						"更新用户账户余额入参异常！", "", "01"));
				return result;
			}
			logger.info("【开始用户账户余额修改】---【saveparams】-----" + uList.toString());
			// 修改用户账户余额
			for (UserAccount ua : uList) {
				int rec = userAccountMapper.updateByUserId(ua);
				if (rec != 1) {
					logger.error("【用户账户余额修改】---【更新用户账户余额失败！】----影响记录数：=" + rec);
					logger.error("【用户账户余额修改】---【参数】----[" + ua.toString()+"]");
					result.put("result", (ResultInfo) ResultUtil.initResult(ResultInfo.ERROR, "useracc_error",
							"更新用户账户余额异常！", "", "01"));
					return result;
				}
			}
			result.put("result", (ResultInfo) ResultUtil.initResult(ResultInfo.SUCCESS, "useracc_success", "更新用户账户余额成功！"));
			logger.info("【用户账户余额修改】---【结束】-----");
			return result;
		} catch (Exception e) {
			logger.error("【用户账户余额修改】---【更新用户账户余额异常！】-----" + Exceptions.getStackTraceAsString(e));
			result.put("result", (ResultInfo) ResultUtil.initResult(ResultInfo.ERROR, "useracc_exception", "更新用户账户余额异常！", "", "01"));
			return result;
		}
	}
	/**
	 * 根据交易订单构造佣金返销记录、余额明细、用户余额
	 * 
	 * @param Commlist
	 *            返销记录
	 * @param balancelist
	 *            返销记录
	 * @param uList
	 *            用户账户
	 * @param tflist
	 *            交易订单
	 * @param userId
	 *            用户id
	 * @param parentOne 一级用户
	 * @param parentTow 二级用户
	 * @param bbRule
	 *            规则配置
	 * @return Map<String, Object>
	 * 
	 */
	public Map<String, Object> createInfoByTranFlow(List<CommissionDetail> Commlist, List<BalanceDetail> balancelist,
			List<UserAccount> uList, List<TransactionFlowPo> tflist, BuyBackRule bbRule, UserInfo parentOne,UserInfo parentTow,
			Integer userId) {
		Map<String, Object> result = new HashMap<String, Object>();
		logger.info("【返销处理】---【构建交易订单构造佣金返销记录、余额明细、用户余额】----【开始】--");
		try {
			for (TransactionFlowPo t : tflist) {
				// 构建返销记录
				CommissionDetail commDetail = new CommissionDetail();
				commDetail.setUserId(t.getUserId());// 当前用户上级id
				commDetail.setMoney(t.getPayPrice());// 更换为消费金额乘以返销点数
				if (parentOne!=null&&t.getUserId()==parentOne.getId()) {
					commDetail.setRebate(bbRule.getOneLevelRebate());// 一级返销点数
					commDetail.setFriendId(userId);// 一级返销为当前用户id
					commDetail.setLevel("01");// 01 一级；02二级
					commDetail.setRemark("邀请好友奖励");
				}
				if (parentTow!=null&&t.getUserId()==parentTow.getId()) {
					commDetail.setRebate(bbRule.getTwoLevelRebate());// 二级返销点数
					commDetail.setFriendId(parentOne.getId());//// 二级返销为当前用户的上级id
					commDetail.setLevel("02");// 01 一级；02二级
					commDetail.setRemark("邀请好友奖励");
				}
				commDetail.setTransactionId(t.getId());// 交易订单id
				commDetail.setCreateTime(DateUtil.getTime());
				Commlist.add(commDetail);
				// 获取用户账户余额
				UserAccount userAcc = userAccountMapper.selectByUserId(t.getUserId());
				if (userAcc == null) {
					logger.error("【返销处理】---【构建交易订单构造佣金返销记录、余额明细、用户余额】---【获取用户余额异常】------");
					result.put("result",(ResultInfo) ResultUtil.initResult(ResultInfo.ERROR, "cibtf_error", "获取用户余额异常！", "", "01"));
					return result;
				}
				logger.info("【返销处理】---【构建交易订单构造佣金返销记录、余额明细、用户余额】---【获取用户余额信息】------" + userAcc.toString());
				// 余额明细
				BalanceDetail balance = new BalanceDetail();
				balance.setUserId(t.getUserId());// 用户id
				balance.setMoney(t.getPayPrice());// 金额
				balance.setIncomeState("01");// 增加
				balance.setTransactionId(t.getId());
				balance.setType("03");// 返销
				balance.setRemark(t.getRemark());
				balance.setCreateTime(DateUtil.getTime());
				balance.setCurBalance(userAcc.getBalance());// 设置用户当前余额
				balancelist.add(balance);
				// 用户账单余额
				UserAccount uAccount = new UserAccount();
				uAccount.setUserId(t.getUserId());// 变更id
				uAccount.setBalance(t.getPayPrice());// 本次变动余额
				uAccount.setVersion(userAcc.getVersion());// 设置当前版矢量
				uList.add(uAccount);
			}
			result.put("result", (ResultInfo) ResultUtil.initResult(ResultInfo.SUCCESS, "cibtf_success", "构建交易订单构造佣金返销记录、余额明细、用户余额成功"));
			logger.info("【返销处理】---【构建交易订单构造佣金返销记录、余额明细、用户余额】----【结束】--");
			return result;
		} catch (Exception e) {
			logger.error("【返销处理】---【构建交易订单构造佣金返销记录、余额明细、用户余额】---【异常】-----" + Exceptions.getStackTraceAsString(e));
			result.put("result", (ResultInfo) ResultUtil.initResult(ResultInfo.ERROR, "cibtf_exception",
					"构建交易订单构造佣金返销记录、余额明细、用户余额异常！", "", "01"));
			return result;
		}

	}
	/**
	 * 保存交易订单
	 * @param tflist 交易订单集合
	 * @param bbRule 返销规则
	 * @param parentOne 一级用户
	 * @param parentTow 二级用户
	 * @param money 支付金额
	 * @return
	 */
	public Map<String, Object> saveTransactionFlow(List<TransactionFlowPo> tflist, BuyBackRule bbRule,
			UserInfo parentOne,UserInfo parentTow, BigDecimal money) {
		Map<String, Object> result = new HashMap<String, Object>();
		logger.info("【返销处理】---【构建返销交易订单】---【开始】---");
		try {
			if (parentOne!=null) {
				logger.info("【返销处理】---【构建返销交易订单】---【当前用户上级id】-----{}",parentOne.getId());
				// 构建一级返销交易订单
				TransactionFlowPo tFlow = new TransactionFlowPo();
				tFlow.setUserId(parentOne.getId());// 当前用户上级id
				// 支付金额乘以返销点数
				BigDecimal OneBackMoney = money.multiply(new BigDecimal(bbRule.getOneLevelRebate())
						.divide(new BigDecimal(100), 3, BigDecimal.ROUND_HALF_UP));
				tFlow.setTotalPrice(OneBackMoney.setScale(2, BigDecimal.ROUND_HALF_UP));// 设置为一级返销金额
				tFlow.setPayPrice(OneBackMoney.setScale(2, BigDecimal.ROUND_HALF_UP));// 设置为一级返销金额
				tFlow.setPriceState("01");// 进
				tFlow.setRemark("邀请好友奖励");
				tFlow.setState("01");// 已支付
				tFlow.setType("02");// 返销
				tFlow.setCreateTime(DateUtil.getTime());
				tFlow.setFinishTime(DateUtil.getTime());
				tFlow.setVersion(0);
				tflist.add(tFlow);
			}
			if (parentTow!=null) {
				logger.info("【返销处理】---【构建返销交易订单】---【当前用户上级的上级id】-----{}",parentTow.getId());
				// 构建一级返销交易订单
				TransactionFlowPo tFlowP = new TransactionFlowPo();
				tFlowP.setUserId(parentTow.getId());// 当前用户上级id
				// 支付金额乘以返销点数
				BigDecimal twoBackMoney = money.multiply(new BigDecimal(bbRule.getTwoLevelRebate())
						.divide(new BigDecimal(100), 3, BigDecimal.ROUND_HALF_UP));
				tFlowP.setTotalPrice(twoBackMoney.setScale(2, BigDecimal.ROUND_HALF_UP));// 设置为二级返销金额
				tFlowP.setPayPrice(twoBackMoney.setScale(2, BigDecimal.ROUND_HALF_UP));// 设置为二级返销金额
				tFlowP.setPriceState("01");// 进
				tFlowP.setRemark("邀请好友奖励");
				tFlowP.setState("01");// 已支付
				tFlowP.setType("02");// 返销
				tFlowP.setCreateTime(DateUtil.getTime());
				tFlowP.setFinishTime(DateUtil.getTime());
				tFlowP.setVersion(0);
				tflist.add(tFlowP);
			}
			if (tflist.size() <= 0) {
				logger.info("【返销处理】---【构建返销交易订单】---【当前用户不存在上级】-----");
				result.put("result", (ResultInfo) ResultUtil.initResult(ResultInfo.ERROR, "tranflow_error", "生成返销交易订单异常！"));
				return result;
			}
			logger.info("【返销处理】---【构建返销交易订单】---【saveparams】-----" + tflist.toString());
			// 保存返销交易订单
			transactionFlowPoMapper.insertTransactionFlows(tflist);
			for (TransactionFlowPo t : tflist) {
				// 构建交易订单编码
				t.setCode(CustomUtil.orderCode(3, t.getId(), t.getUserId()));
			}
			logger.info("【返销处理】---【构建返销交易订单】---【updateparams】-----" + tflist.toString());
			for (TransactionFlowPo tfp : tflist) {
				int upRec = transactionFlowPoMapper.updateCode(tfp);
				if (upRec != 1) {
					logger.info("【返销处理】---【构建返销交易订单】---【插入交易订单编码失败！】----影响记录数：=" + upRec);
					result.put("result",(ResultInfo) ResultUtil.initResult(ResultInfo.ERROR, "tranflow_error", "生成返销交易订单异常！", "", "01"));
					return result;
				}
			}
			logger.info("【返销处理】---【构建返销交易订单】---【结束】-----");
			result.put("result", (ResultInfo) ResultUtil.initResult(ResultInfo.SUCCESS, "tranflow_success", "交易订单生成成功"));
			return result;
		} catch (Exception e) {
			logger.error("【返销处理】---【构建返销交易订单】---【生成返销交易订单异常！】-----" + Exceptions.getStackTraceAsString(e));
			result.put("result",(ResultInfo) ResultUtil.initResult(ResultInfo.ERROR, "tranflow_exception", "生成返销交易订单异常！", "", "01"));
			return result;
		}
	}
	/**
	 * 发送返销消息
	 * @param tflist 交易订单
	 * @param parentOne 一级用户
	 * @param parentTow 二级用户
	 */
	public void sendMsg(List<TransactionFlowPo> tflist, UserInfo parentOne,UserInfo parentTow,Integer userId){
		logger.info("【发送消息】---【开始】-----");
		try {
			if (tflist.size() <= 0) {
				logger.error("【发送消息】---【构建参数异常】----");
			}
			// 根据交易订单构建 发送消息体
			for (TransactionFlowPo tfp : tflist) {
				SysMessage msg=null;
				if (parentOne!=null&&tfp.getUserId()==parentOne.getId()) {//一级返销有当前消费者提供
					//获取当前消费者的phone
					UserInfo user=userService.getUser(userId);
					if(user==null){
						logger.info("【发送消息】---【一级返销获取当前消费用户号码异常】-----");
						continue;
					}
					msg=CommMsgUtil.getMsgForComm(tfp.getUserId(), tfp.getPayPrice(), user.getPetName());
				}
				if (parentTow!=null&&tfp.getUserId()==parentTow.getId()) {//二级返销由一级提供
					//获取当前消费用户的上级phone
					UserInfo userPanent=userService.getParentOne(userId);
					if(userPanent==null){
						logger.info("【发送消息】---【二级返销获取一级用户号码异常】-----");
						continue;
					}
					msg=CommMsgUtil.getMsgForComm(tfp.getUserId(), tfp.getPayPrice(), userPanent.getPetName());
				}
				logger.info("【发送消息】---【sendparams】-----" + msg.toString());
				try {
					sysMessageService.insert(msg);
				} catch (Exception e) {
					logger.error("【发送消息】---【异常】-----" + Exceptions.getStackTraceAsString(e));
					continue;
				}
			}
			logger.info("【发送消息】---【结束】-----");
		} catch (Exception e) {
			logger.error("【发送消息】---【异常】-----" + Exceptions.getStackTraceAsString(e));
		}
	}
	/**
	 * 发送返销消息
	 * @param tflist 交易订单
	 * @param parentOne 一级用户
	 * @param parentTow 二级用户
	 */
	public void sendSms(List<TransactionFlowPo> tflist){
		logger.info("【发送短信】---【开始】-----");
		if (tflist.size() <= 0) {
			logger.error("【发送短信】---【构建参数异常】----");
		}
		// 根据交易订单构建 发送消息体
		for (TransactionFlowPo tfp : tflist) {
			try {
				String params=DateUtil.getSmsTime()+","+tfp.getPayPrice().setScale(2, BigDecimal.ROUND_HALF_UP);
				logger.info("【发送短信】---【params】---{}---{}",tfp.getUserId(),params);
				Map<String, Object> result=smsCodeService.sendBonusToDB("reward", tfp.getUserId(), params);
				ResultInfo creResult = (ResultInfo) result.get("result");
				if (ResultInfo.ERROR.equals(creResult.getState())) {
					logger.error("【发送短信】---【失败】-----{}---{}",creResult.getState(),creResult.getMessage());
				}
			} catch (Exception e) {
				logger.error("【发送短信】---【异常】-----" + Exceptions.getStackTraceAsString(e));
				continue;
			}
		}
		logger.info("【发送短信】---【结束】-----");
	}
}
